<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">
          <a-col
            :md="3"
            :sm="6"
          >
            <a-form-item>
              <a-input
                v-model="queryParam.agentName"
                placeholder="代理名称"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="3"
            :sm="6"
          >
            <a-form-item>
              <a-select
                v-model="queryParam.effective"
                placeholder="代理状态"
                allowClear
                @change="change(queryParam.effective)"
              >
                <a-select-option
                  v-for="(value, name) in statusOptions"
                  :key="name"
                  :value="name"
                >
                  {{ value.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :md="3"
            :sm="6"
          >
            <span class="table-page-search-submitButtons">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
              >查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="() => queryParam = {}"
              >重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button
        type="primary"
        icon="plus"
        @click="handleAddBanner()"
      >新建</a-button>
      <!-- <a-button type="default" icon="download">导出</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }} alert</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
          <a-menu-item key="2"><a-icon type="lock" />锁定</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>-->
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="userId"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <!-- <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>-->
      <span
        slot="status"
        slot-scope="text"
      >
        <a-badge
          :status="text | status('enterpriseStatus', 'status')"
          :text="text | status('enterpriseStatus', 'text')"
        />
      </span>
      <span
        slot="description"
        slot-scope="text"
      >
        <ellipsis
          :length="12"
          tooltip
        >{{ text }}</ellipsis>
      </span>
      <span
        slot="feeMode"
        slot-scope="text"
      >
        <span>{{ text === 1 ? '开票金额' : '实发金额' }}</span>
      </span>
      <span
        slot="feeRate"
        slot-scope="text, record"
      >
        <template v-if="record.feeRateType === 1">
          <span>{{ (record.feeRate * 100).toFixed(2) }}%</span>
        </template>
        <template v-else>
          <a @click="showFeeRate(record)">阶梯费率</a>
        </template>
      </span>
      <span
        slot="isOpenXbd"
        slot-scope="boolean"
      >
        <span>{{ boolean===true?'已开通':'未开通' }}</span>
      </span>
      <span
        slot="createdTime"
        slot-scope="text"
      >{{ text | moment }}</span>
      <span
        slot="feeRateList"
        slot-scope="text, record"
        @click="showType(record)"
      > <a>点击查看</a></span>
      <span
        slot="action"
        slot-scope="text, record"
      >
        <template>
          <a @click="handleAddBanner(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleSub(record)">{{ record.effective === '0' ? '解冻' : '冻结' }}</a>
        </template>

      </span>
    </s-table>
    <list-form-modal ref="listFormModal" />
    <a-modal 
    title="渠道价列表"
    width="40%"
    :visible="visibleType"
  
    @ok="visibleType = false"
    @cancel="visibleType = false"
    destroyOnClose
    >
    <a-table
    :bordered="false"
                :columns="typeColumns"
                rowKey="key"
                :pagination="false"
                :data-source="typeTable"
              >
     </a-table>
    </a-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { freezeEnterprise1,getAgentUser,addAgentUser,addAgentUpdate } from '@/api/manage'
import { enterpriseStatus, payStatus } from '@/utils/statusFilters'
import ListFormModal from './components/listFormModal'

export default {
  name: 'TableListPage',
  components: {
    STable,
    Ellipsis,
    ListFormModal
  },
  data() {
    return {
      statusOptions: enterpriseStatus,
      payOptions: payStatus,

      mdl: {},
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        // {
        //   title: '#',
        //   scopedSlots: { customRender: 'serial' }
        // },
        {
          title: 'ID',
          dataIndex: 'userId'
        },
        {
          title: '用户名',
          dataIndex: 'username'
          // sorter: true
          // needTotal: true,
          // customRender: (text) => text + ' 次'
        },
        {
          title: '代理名称',
          dataIndex: 'agentName'
        },
        {
          title: '手机号',
          dataIndex: 'phone'
        },
        // {
        //   title: '渠道价',
        //   dataIndex: 'feeRate',
        //   customRender: (text, row, index) => {
        //     if (text!=null){
        //       switch (text) {
        //         default:
        //           return text+'%'
        //       }
        //     }
        //   }
        // },
        // {
        //   title: '发票类型',
        //   dataIndex: 'invoiceType',
        //   customRender: (text, row, index) => {
        //     switch (text) {
        //       case 1:
        //         return '增值税普通发票'
        //       case 2:
        //         return '增值税专用发票'
        //       case 3:
        //         return '增值税差额普票'
        //       case 4:
        //         return '增值税普通发票/增值税专用发票'
        //     }
        //   }
        // },
        {
          title: '渠道价',
          dataIndex: 'feeRateList',
          scopedSlots: { customRender: 'feeRateList' },
          width: '120px',
        },
        {
          title: '销售人员',
          dataIndex: 'salesName',
          width: '100px',
        },
        {
          title: '运营人员',
          dataIndex: 'operationsManagerName',
          width: '100px',
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime'
          // scopedSlots: { customRender: 'createdTime' }
        },
        {
          title: '编辑时间',
          dataIndex: 'modifiedTime'
          // scopedSlots: { customRender: 'createdTime' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        let filterParams = this.queryParam
        if (this.queryParam.createdTime) {
          filterParams = Object.assign({}, this.queryParam, {
            startTime: `${this.queryParam.createdTime[0]} 00:00:00`,
            endTime: `${this.queryParam.createdTime[1]} 23:59:59`
          })
        }
        delete filterParams.createdTime
        console.log(filterParams)
        return getAgentUser(Object.assign(parameter, filterParams)).then(res => {
          return res.resData
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false,
      freezing: false,
      feeRateRangeList: [],
      feeRateVisible: false,
      visibleType: false,
      typeTable: [],
      typeColumns: [
        {
          title: '渠道价',
          align: 'center',
          dataIndex: 'feeRate',
          width: 180,
          customRender: (text, row, index) => {
            if (text!=null){
              switch (text) {
                default:
                  return text+'%'
              }
            }
          },
        },
        {
          title: '发票类型',
          dataIndex: 'invoiceType',
          customRender: (text, row, index) => {
            switch (text) {
              case 1:
                return '增值税普通发票'
              case 2:
                return '增值税专用发票'
              case 3:
                return '增值税差额普票'
              case 4:
                return '增值税普通发票/增值税专用发票'
            }
          }
        }
      ],
    }
  },
  created() {
    // this.tableOption()
    // getRoleList({ t: new Date() })
  },
  methods: {
    showType (info) {
      console.log(info)
      this.typeTable = info.feeRateList
      this.visibleType = true
    },
    change(value) {
      if (value === undefined) {
        this.queryParam = {}
      }
    },
    handleEdit(record) {
      this.$router.push({
        name: 'EnterpriseEdit',
        params: {
          enterpriseId: record.enterpriseId
        }
      })
    },
    handleSub(record) {
      if (this.freezing) {
        return
      }
      const type = record.effective === '0' ? '解冻' : '冻结'
      this.freezing = true
      console.log(record)
      freezeEnterprise1({
        agentId: record.userId,
        isFreeze: !Boolean(+record.effective)
      })
        .then(res => {
          this.$refs.table.refresh()
          this.$message.success(`${record.username}${type}成功`)
        })
        .finally(() => {
          this.freezing = false
        })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    showFeeRate(record) {
      this.feeRateRangeList = record.feeRateRangeList || []
      this.feeRateVisible = true
    },

    handleAddBanner (methodsAction = null) {
      if(methodsAction){
        this.$refs.listFormModal.show(this.handleActionModifyRole,methodsAction)
      }else{
        this.$refs.listFormModal.show(this.handleActionAddBanner,methodsAction)
      }
    },
    handleActionAddBanner (banner, callback) {
      addAgentUser(banner).then(res => {
        callback()
        this.$message.success('添加成功')
        this.$refs.table.refresh()
      }).catch(err => {
        callback(err)
      })
    },
    handleActionModifyRole (banner, callback) {
      if(!banner.password){
        delete banner.password
      }
      delete banner.createdTime
      delete banner.modifiedTime
      console.log(banner)
      addAgentUpdate(banner).then(res => {
        callback()
        this.$message.success('修改成功')
        this.$refs.table.refresh()
      }).catch(err => {
        callback(err)
      })
    }
  }
}
</script>
